package com.easyhouse.controller;

import com.easyhouse.common.pojo.Product;
import com.easyhouse.common.pojo.ProductR;
import com.easyhouse.common.vo.EasyUIResult;
import com.easyhouse.common.vo.SysResult;
import com.easyhouse.mapper.ProductMapper;
import com.easyhouse.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product/manage")
public class ProductController {
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired
    private ProductService productService;

    @RequestMapping("/item/{houseId}")
    public Product queryOneProdcut(@PathVariable("houseId") String houseId){
        Product product = productService.queryByProductId(houseId);
        return product;
    }
    @RequestMapping("/test")
    public String test(){
        List<Product> products = productMapper.selectAllProduct();
        for (Product product : products) {
            System.out.println(product);
        }
        return "success";
    }
    @RequestMapping("/pageManage")
    public EasyUIResult queryByPage(Integer page, Integer rows){
        //
        EasyUIResult result = productService.queryByPage(page,rows);
        return result;
    }
    @RequestMapping("/pageGet")
    public EasyUIResult queryByPageget(Integer page, Integer rows){
        //
        //EasyUIResult result = productService.queryByPage(page,rows);
        EasyUIResult result = productService.queryByPageget(page,rows);
        return result;
    }

    @RequestMapping("/save")
    public SysResult save(ProductR productR){
        Product product = productR.toProduct();
        //System.out.println(product);
        SysResult sysResult = productService.saveProduct(product);
        return sysResult;
    }
    @RequestMapping("/update")
    public SysResult editProduct(Product product){
        try{
            productService.editProduct(product);
            return SysResult.ok();
        }catch (Exception e){
            return SysResult.build(201,"更新失败",null);
        }
    }
    @RequestMapping("/delete")
    public SysResult deleteProduct(String ids){
        try {
            productService.deleteProduct(ids);
            return SysResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(201,"删除商品失败",null);
        }
    }

    @RequestMapping("/list")
    public List<Product> list(){
        try {
            return productService.selectSeckillProduct();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
